package com.xrh.xdemo.service.controllers;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xrh.core.annotation.InterfacePermission;
import com.xrh.core.annotation.RepeatRequest;
import com.xrh.core.bean.CommonPage;
import com.xrh.core.bean.CommonResult;
import com.xrh.core.enums.ResultCodeEnum;
import com.xrh.core.exceptions.BusinessException;
import com.xrh.xdemo.api.dto.entity.SysUserDTO;
import com.xrh.xdemo.api.dto.extend.SysUserBindRoleInfoDTO;
import com.xrh.xdemo.api.dto.extend.SysUserRoleInfoDTO;
import com.xrh.xdemo.api.request.addRequest.SysUserAddRequest;
import com.xrh.xdemo.api.request.queryRequest.SysUserQueryRequest;
import com.xrh.xdemo.api.request.updateRequest.ModifyPasswordRequest;
import com.xrh.xdemo.api.request.updateRequest.SysUserUpdateRequest;
import com.xrh.xdemo.api.request.updateRequest.UserBindRolesRequest;
import com.xrh.xdemo.service.annotations.OperateTrace;
import com.xrh.xdemo.service.service.SysUserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(tags="用户API")
@RestController
@RequestMapping("sysUser")
@Slf4j
public class SysUserController {

    @Resource
    private SysUserService sysUserService;

    /**
   	 * @function 用户绑定角色
   	 * @param bindRequest
   	 * @author 肖荣辉
   	 * @date 2023-12-08 15:17:26
   	*/
   	@ApiOperation("用户绑定角色")
   	@PostMapping("/userBindRoles")
   	public  CommonResult<Void> userBindRoles(@Valid @RequestBody UserBindRolesRequest userBindRolesRequest){
   		
   		try {
   		
   			sysUserService.userBindRoles(userBindRolesRequest);
   			
   			return CommonResult.success();
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("userBindRoles 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		}
   		
   	}
   	
    /**
   	 * @function 新增用户
   	 * @param sysUserAddRequest
   	 * @author 肖荣辉
   	 * @date 2023-12-08 15:17:26
   	*/
   	@ApiOperation("新增用户")
   	@PostMapping("/addSysUser")
   	@InterfacePermission(name="新增用户" , value ="/sysUser/addSysUser")
   	@RepeatRequest(timeout = 2)
	@OperateTrace(operateType = "新增" , businessType = "用户" ,  businessNoFromReturnResult = true , businessNoKeyPath = "data" , titleKeyPath  = "userName")
   	public  CommonResult<Long> addSysUser(@Valid @RequestBody SysUserAddRequest sysUserAddRequest){
   		
   		try {
   		
   			Long id = sysUserService.addSysUser(sysUserAddRequest);
   			
   			return CommonResult.success(id);
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("addSysUser 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		}
   		
   	}
   	
   	
   	/**
   	 * @function 修改密码
   	 * @param modifyPasswordRequest
   	 * @author 肖荣辉
   	 * @date 2023-12-08 15:17:26
   	 */
   	@ApiOperation("修改密码")
   	@PostMapping("/modifyPassword")
	@OperateTrace(operateType = "修改" , businessType = "用户密码" ,   businessNoKeyPath = "data")
   	public  CommonResult<Void> modifyPassword(@Valid @RequestBody ModifyPasswordRequest modifyPasswordRequest){
   		
   		try {
   			
   			sysUserService.modifyPassword(modifyPasswordRequest);
   			
   			return CommonResult.success();
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("modifyPassword 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		}
   		
   	}
   	
   	/**
   	 * @function 启用禁用用户
   	 * @author 肖荣辉
   	 * @date 2023-12-08 15:17:26
   	 */
   	@ApiOperation("启用禁用用户")
   	@PostMapping("/enableUser")
   	@ApiImplicitParams(value = {
   			@ApiImplicitParam(name = "id" ,value = "用户id"),
   			@ApiImplicitParam(name = "enableFlag" ,value = "启用禁用标志(0:禁用,1:启用)")
   	})
   	@OperateTrace(operateType = "启用/禁用" , businessType = "用户" ,   businessNoKeyPath = "id")
   	public  CommonResult<Void> enableUser(Long id , Integer enableFlag){
   		
   		try {
   			
   			sysUserService.enableUser(id , enableFlag);
   			
   			return CommonResult.success();
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("enableUser 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		}
   		
   	}
   	
   	/**
   	 * @function 重置密码
   	 * @author 肖荣辉
   	 * @date 2023-12-08 15:17:26
   	 */
   	@ApiOperation("重置密码")
   	@PostMapping("/resetPassword")
   	@ApiImplicitParam(name = "id" ,value = "用户id")
   	@InterfacePermission(name="重置密码" , value = "/sysUser/resetPassword")
   	@OperateTrace(operateType = "重置" , businessType = "用户密码" ,   businessNoKeyPath = "id")
   	public  CommonResult<Void> resetPassword(Long id){
   		
   		try {
   			
   			sysUserService.resetPassword(id);
   			
   			return CommonResult.success();
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("resetPassword 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		}
   		
   	}
   	
   	/**
   	 * @function 删除用户
   	 * @author 肖荣辉
   	 * @date 2023-12-08 15:17:26
   	*/
   	@ApiOperation("删除用户")
   	@ApiImplicitParam(name = "id", value = "主键ID", required = true)
   	@PostMapping("/deleteSysUserById")
   	@InterfacePermission(name="删除用户" , value ="/sysUser/deleteSysUserById")
   	@OperateTrace(operateType = "删除" , businessType = "用户" ,   businessNoKeyPath = "id")
   	public CommonResult<Void> deleteSysUserById(Long  id){
   		
   		try {
   			
   			if(id == null)
   				return CommonResult.failed("id不能为空");
   			
   			sysUserService.deleteSysUserById(id);
   			
   			return CommonResult.success();
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("deleteSysUser发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		} 
   		
   	}
   	
   	
   	/**
   	 * @function 更新用户
   	 * @param sysUserUpdateRequest
   	 * @author 肖荣辉
   	 * @date 2023-12-08 15:17:26
   	*/
   	@ApiOperation("更新用户")
   	@PostMapping("/updateSysUser")
   	@InterfacePermission(name="编辑用户" , value ="/sysUser/updateSysUser")
   	@OperateTrace(operateType = "修改" , businessType = "用户" ,   businessNoKeyPath = "id" , titleKeyPath = "userName")
   	public  CommonResult<Void> updateSysUser(@Valid @RequestBody SysUserUpdateRequest sysUserUpdateRequest){
   		
   		try {
   			
   			sysUserService.updateSysUser(sysUserUpdateRequest);
   			
   			return CommonResult.success();
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("updateSysUser 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		} 
   		
   	}
   	
   	/**
   	 * @function 修改个人信息
   	 * @param sysUserUpdateRequest
   	 * @author 肖荣辉
   	 * @date 2023-12-08 15:17:26
   	 */
   	@ApiOperation("修改个人信息")
   	@PostMapping("/modifyPersonInfo")
   	@OperateTrace(operateType = "修改" , businessType = "个人信息" ,   businessNoKeyPath = "id" , titleKeyPath = "userName")
   	public  CommonResult<Void> modifyPersonInfo(@Valid @RequestBody SysUserUpdateRequest sysUserUpdateRequest){
   		
   		try {
   			
   			sysUserService.modifyPersonInfo(sysUserUpdateRequest);
   			
   			return CommonResult.success();
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("updateSysUser 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		} 
   		
   	}
   	
   	/**
   	 * @function 根据ID获取对象
   	 * @param id
   	 * @author 肖荣辉
   	 * @date 2023-12-08 15:17:26
   	*/
   	@ApiOperation("获取用户信息")
    @ApiImplicitParam(name = "id", value = "主键ID", required = true)
   	@PostMapping("/findSysUserById")
   	public CommonResult<SysUserDTO> findSysUserById(Long id){
   		
   		try {
   			
   			if(id == null)
   				return CommonResult.failed("id不能为空");
   				
   			SysUserDTO dto =  sysUserService.findSysUserById(id);
   			
   			return CommonResult.success(dto);
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("findSysUserById 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		} 
   		
   		
   	}
   	
   	/**
   	 * @function 查询用户列表
   	 * @author 肖荣辉
   	 * @param sysUserQueryRequest
   	 * @date 2023-12-08 15:17:26
   	*/
   	@ApiOperation("获取用户列表")
   	@PostMapping("/findSysUserList")
   	public CommonResult<CommonPage<SysUserDTO>> findSysUserList(@RequestBody  SysUserQueryRequest sysUserQueryRequest){
   		
   		try {
   			
   			return sysUserService.findSysUserList(sysUserQueryRequest);
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("findSysUserList 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		} 
   		
   	}
   	
   	/**
   	 * @function 获取用户携带角色信息列表
   	 * @author 肖荣辉
   	 * @param sysUserQueryRequest
   	 * @date 2023-12-08 15:17:26
   	*/
   	@ApiOperation("获取用户携带角色信息列表")
   	@PostMapping("/findSysUserWithRoleInfoList")
   	public CommonResult<CommonPage<SysUserRoleInfoDTO>> findSysUserWithRoleInfoList(@RequestBody  SysUserQueryRequest sysUserQueryRequest){
   		
   		try {
   			 
   			return sysUserService.findSysUserWithRoleInfoList(sysUserQueryRequest);
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("findSysUserWithRoleInfoList 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		} 
   		
   	}
   	
   	/**
   	 * @function 获取携带绑定角色标志的用户信息列表
   	 * @author 肖荣辉
   	 * @param sysUserQueryRequest
   	 * @date 2023-12-08 15:17:26
   	 */
   	@ApiOperation("获取携带绑定角色标志的用户信息列表")
   	@PostMapping("/findSysUserBindRoleInfoList")
   	public CommonResult<CommonPage<SysUserBindRoleInfoDTO>> findSysUserBindRoleInfoList(@RequestBody  SysUserQueryRequest sysUserQueryRequest){
   		
   		try {
   			
   			return sysUserService.findSysUserBindRoleInfoList(sysUserQueryRequest);
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("findSysUserBindRoleInfoList 发生错误 ==> " , e);
   			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
   		} 
   		
   	}
   	
   	

}
